package com.we.connect.weconnect.controller.remind;

import com.we.connect.weconnect.model.common.ResultModel;
import com.we.connect.weconnect.model.remind.vo.RemindInfoVo;
import com.we.connect.weconnect.model.system.SendUserInfo;
import com.we.connect.weconnect.model.system.SysUser;
import com.we.connect.weconnect.service.RemindInfoService;
import com.we.connect.weconnect.service.SendUserInfoService;
import com.we.connect.weconnect.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/remind")
public class RemindInfoController {

    @Autowired
    private RemindInfoService remindInfoService;

    @Autowired
    private SendUserInfoService sendUserInfoService;

    @PostMapping("/addRemindInfo")
    public String addRemindInfo(@ModelAttribute("remindInfoVo")RemindInfoVo remindInfoVo, Model model, HttpServletRequest request){
        SysUser loginSysUser = (SysUser) UserUtils.getSysUser(request, "loginSysUser");
        remindInfoVo.setUserId(loginSysUser.getUserId());

        Boolean isAddSuccess = remindInfoService.addRemindInfo(remindInfoVo);
        if(isAddSuccess){
            return "redirect:/remind";
        }
        model = ResultModel.fail("保存", "新增提醒", "/addRemind/add/0", model);
        return "common/tip";
    }

    @PostMapping("/editRemindInfo")
    public String editRemindInfo(@ModelAttribute("remindInfoVo")RemindInfoVo remindInfoVo, Model model, HttpServletRequest request){
        SysUser loginSysUser = (SysUser) UserUtils.getSysUser(request, "loginSysUser");
        remindInfoVo.setUserId(loginSysUser.getUserId());
        Boolean isEditSuccess = remindInfoService.editRemindInfo(remindInfoVo);
        if(isEditSuccess){
            return "redirect:/remind";
        }
        model = ResultModel.fail("修改", "修改提醒", "/addRemind/edit/" + remindInfoVo.getTableId(), model);
        return "common/tip";
    }

    @GetMapping("/deleteRemindInfo/{id}")
    public String deleteRemindInfo(@PathVariable("id") Long id, Model model){
        Boolean isDelSuccess = remindInfoService.deleteRemindInfo(id);
        if(isDelSuccess){
            return "redirect:/remind";
        }
        model = ResultModel.fail("删除", "提醒列表", "/remind", model);
        return "common/tip";
    }

}
